package com.example.huajia.wx.service.impl;

import com.example.huajia.wx.db.dao.TbProductDao;
import com.example.huajia.wx.service.ProductService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import static com.example.huajia.wx.common.util.RedisKey.PRODUCT_ALL_KEY;

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private TbProductDao productDao;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public ArrayList<HashMap> searchAllProductByPage(HashMap param) {
        //redis无数据，进到这里
        ArrayList<HashMap> list = productDao.searchAllProductByPage(param);
        //查询完数据之后，要将其放入redis中去
        String key = PRODUCT_ALL_KEY;
        for (HashMap map : list){
            redisTemplate.opsForList().rightPush(key, map);
        }
        redisTemplate.expire(key, 300L, TimeUnit.MINUTES);
        //返回数据
        return list;
    }
}
